var getAxis = function(img){
    const axis = [];
    const canvas = document.createElement('canvas');
    const context = canvas.getContext('2d');
    const width = img.width = 60;
    const height = img.height = 30;
    context.drawImage(img, 0, 0);
    const imgData = context.getImageData(2, 4, width, height);
    for(let y = 0; y < imgData.height; y++){
        for(let x = 0; x < imgData.width; x++){
            const rIndex = (y * 4) * imgData.width + (x * 4);
            const gIndex = rIndex + 1;
            const bIndex = rIndex + 2;
            const r = imgData.data[rIndex]; 
            const g = imgData.data[gIndex]; 
            const b = imgData.data[bIndex];
            imgData.data[rIndex] = imgData.data[gIndex] = imgData.data[bIndex] = r+g+b > 360 ? 0 : 255; 
        }
    }

    context.putImageData(imgData, 0, 0);

    let _width = 14;
    let _height = imgData.height;
    let xaxis = _width;
    let singleImgData = context.getImageData(0, 0, _width, _height);
    let compare = true;
    for(let y = 0; y < singleImgData.height; y++){
        for(let x = 0; x < singleImgData.width; x++){
            const rIndex = (y * 4) * singleImgData.width + (x * 4);
            const gIndex = rIndex + 1;
            const bIndex = rIndex + 2;
            if(singleImgData.data[rIndex] === 255 && singleImgData.data[gIndex] === 255 && singleImgData.data[bIndex] === 255){
                xaxis = Math.min(xaxis, x)
                if(xaxis === 0){
                    compare = false
                    break;
                }
            }
        }
        if(!compare){
            break;
        }
    }
    if(xaxis > 0){
        singleImgData = context.getImageData(xaxis, 0, _width - xaxis, _height);
    }

    for(let y = 0; y < singleImgData.height; y++){
        for(let x = 0; x < singleImgData.width; x++){
            const rIndex = (y * 4) * singleImgData.width + (x * 4);
            const gIndex = rIndex + 1;
            const bIndex = rIndex + 2;
            if(singleImgData.data[rIndex] === 255 && singleImgData.data[gIndex] === 255 && singleImgData.data[bIndex] === 255){
                axis.push(`${x},${y}`)
            }
        }
    }

    return JSON.stringify(axis)
}

//下划线开头是开发环境验证码坐标
var axisMap = {
    '1':["6,0","3,1","4,1","5,1","6,1","0,2","1,2","2,2","3,2","4,2","5,2","6,2","0,3","4,3","5,3","6,3","4,4","5,4","6,4","4,5","5,5","6,5","4,6","5,6","6,6","4,7","5,7","6,7","4,8","5,8","6,8","4,9","5,9","6,9","4,10","5,10","6,10","4,11","5,11","6,11","4,12","5,12","6,12","10,12","4,13","5,13","6,13","10,13","4,14","5,14","6,14","4,15","5,15","6,15","4,16","5,16","6,16","4,17","5,17","6,17","0,18","1,18","2,18","3,18","4,18","5,18","6,18","7,18","8,18","9,18","10,18","0,19","1,19","2,19","3,19","4,19","5,19","6,19","7,19","8,19","9,19","10,19"],
    '_1':["3,1","1,2","2,2","3,2","2,3","3,3","2,4","3,4","2,5","3,5","2,6","3,6","2,7","3,7","2,8","3,8","2,9","3,9","2,10","3,10","11,10","2,11","3,11","11,11","2,12","3,12","11,12","2,13","3,13","11,13","2,14","3,14","2,15","3,15","2,16","3,16","2,17","3,17","2,18","3,18","0,19","1,19","2,19","3,19","4,19","5,19"],
    
    '2':["5,0","6,0","7,0","3,1","4,1","5,1","6,1","7,1","8,1","9,1","1,2","2,2","9,2","10,2","11,2","10,3","11,3","10,4","11,4","12,4","10,5","11,5","12,5","10,6","11,6","12,6","10,7","11,7","9,8","10,8","11,8","8,9","9,9","10,9","8,10","9,10","6,11","7,11","5,12","6,12","7,12","4,13","5,13","3,14","4,14","2,15","3,15","1,16","2,16","1,17","2,17","0,18","1,18","2,18","3,18","4,18","5,18","6,18","7,18","8,18","9,18","10,18","11,18","12,18","0,19","1,19","2,19","3,19","4,19","5,19","6,19","7,19","8,19","9,19","10,19","11,19","12,19"],
    '_2':["3,1","4,1","5,1","1,2","2,2","3,2","4,2","5,2","6,2","7,2","0,3","7,3","8,3","7,4","8,4","8,5","9,5","8,6","9,6","8,7","8,8","8,9","0,18","1,18","2,18","3,18","4,18","5,18","6,18","7,18","8,18","9,18","0,19","1,19","2,19","3,19","4,19","5,19","6,19","7,19","8,19","9,19"],
    
    '3':["4,0","5,0","6,0","7,0","1,1","2,1","3,1","4,1","5,1","6,1","7,1","8,1","9,1","8,2","9,2","10,2","9,3","10,3","11,3","9,4","10,4","11,4","9,5","10,5","11,5","9,6","10,6","9,7","10,7","8,8","9,8","3,9","4,9","5,9","6,9","7,9","3,10","4,10","5,10","6,10","7,10","8,10","0,11","9,11","10,11","0,12","10,12","11,12","13,12","0,13","10,13","11,13","12,13","13,13","0,14","10,14","11,14","12,14","10,15","11,15","12,15","10,16","11,16","1,17","8,17","9,17","10,17","11,17","1,18","2,18","3,18","4,18","5,18","6,18","7,18","8,18","9,18","10,18","5,19","6,19","7,19"],
    '_3':["3,1","4,1","5,1","1,2","2,2","3,2","4,2","5,2","6,2","7,2","0,3","6,3","7,3","7,4","8,4","7,5","8,5","7,6","7,7","6,8","5,9","6,9","7,9","2,10","3,10","4,10","5,10","6,10","7,10","8,10","6,11","7,11","8,11","7,12","8,12","9,12","8,13","9,13","8,14","9,14","8,15","8,16","0,18","0,19","1,19","2,19","3,19","4,19"],
    
    '4':["9,0","10,0","11,0","8,1","9,1","10,1","11,1","8,2","9,2","10,2","11,2","7,3","9,3","10,3","11,3","6,4","7,4","9,4","10,4","11,4","5,5","6,5","9,5","10,5","11,5","5,6","9,6","10,6","11,6","4,7","9,7","10,7","11,7","3,8","9,8","10,8","11,8","2,9","3,9","9,9","10,9","11,9","2,10","9,10","10,10","11,10","1,11","9,11","10,11","11,11","0,12","1,12","2,12","3,12","4,12","5,12","6,12","7,12","8,12","9,12","10,12","11,12","12,12","13,12","0,13","1,13","2,13","3,13","4,13","5,13","6,13","7,13","8,13","9,13","10,13","11,13","12,13","13,13","9,14","10,14","11,14","9,15","10,15","11,15","9,16","10,16","11,16","9,17","10,17","11,17","9,18","10,18","11,18","9,19","10,19","11,19"],
    '_4':["8,1","7,2","8,2","7,3","8,3","7,4","8,4","7,5","8,5","7,6","8,6","13,6","7,7","8,7","13,7","7,8","8,8","13,8","7,9","8,9","1,10","7,10","8,10","7,11","8,11","7,12","8,12","0,13","1,13","2,13","3,13","4,13","5,13","6,13","7,13","8,13","9,13","10,13","0,14","1,14","2,14","3,14","4,14","5,14","6,14","7,14","8,14","9,14","10,14","7,15","8,15","7,16","8,16","7,17","8,17","7,18","8,18","7,19","8,19","13,19"],
    
    '5':["0,0","1,0","2,0","3,0","4,0","5,0","6,0","7,0","8,0","9,0","10,0","11,0","0,1","1,1","2,1","3,1","4,1","5,1","6,1","7,1","8,1","9,1","10,1","11,1","0,2","1,2","0,3","1,3","0,4","1,4","0,5","1,5","0,6","1,6","0,7","1,7","2,7","3,7","4,7","0,8","1,8","2,8","3,8","4,8","5,8","6,8","7,8","6,9","7,9","8,9","9,9","8,10","9,10","10,10","9,11","10,11","9,12","10,12","11,12","9,13","10,13","11,13","9,14","10,14","11,14","9,15","10,15","8,16","9,16","10,16","8,17","9,17","0,18","1,18","2,18","3,18","4,18","5,18","6,18","7,18","8,18","2,19","3,19","4,19","5,19"],
    '_5':["3,1","4,1","5,1","6,1","7,1","8,1","3,2","4,2","5,2","6,2","7,2","8,2","2,3","1,6","2,6","0,7","1,7","2,7","3,7","4,7","5,7","4,8","5,8","6,8","6,9","7,9","7,10","8,10","8,11","8,12","9,12","8,13","9,13","8,14","9,14","8,15","8,16","7,17","0,18","1,19","2,19","3,19","13,19"],
    
    '6':["6,0","7,0","8,0","9,0","4,1","5,1","6,1","7,1","8,1","9,1","10,1","11,1","12,1","3,2","4,2","12,2","2,3","3,3","2,4","3,4","1,5","2,5","1,6","2,6","1,7","2,7","6,7","7,7","8,7","9,7","0,8","1,8","2,8","4,8","5,8","6,8","7,8","8,8","9,8","10,8","11,8","0,9","1,9","2,9","3,9","4,9","10,9","11,9","12,9","0,10","1,10","2,10","3,10","11,10","12,10","0,11","1,11","2,11","11,11","12,11","0,12","1,12","2,12","11,12","12,12","1,13","2,13","11,13","12,13","1,14","2,14","11,14","12,14","1,15","2,15","11,15","12,15","2,16","3,16","11,16","12,16","2,17","3,17","4,17","10,17","11,17","4,18","5,18","6,18","7,18","8,18","9,18","10,18","6,19","7,19","8,19"],
    '_6':["8,1","9,1","10,1","5,2","6,2","4,3","3,4","2,5","1,6","0,7","1,7","0,8","4,8","5,8","6,8","0,9","1,9","7,9","8,9","0,10","8,10","9,10","0,11","9,11","0,12","9,12","10,12","0,13","9,13","10,13","0,14","9,14","10,14","0,15","9,15","0,16","1,16","9,16","1,17","2,18","4,19","5,19"],
    
    '7':["0,0","1,0","2,0","3,0","4,0","5,0","6,0","7,0","8,0","9,0","10,0","11,0","0,1","1,1","2,1","3,1","4,1","5,1","6,1","7,1","8,1","9,1","10,1","11,1","10,2","11,2","10,3","11,3","9,4","10,4","9,5","8,6","9,6","7,7","8,7","7,8","6,9","7,9","5,10","6,10","5,11","6,11","4,12","5,12","4,13","5,13","3,14","4,14","3,15","4,15","2,16","3,16","4,16","2,17","3,17","2,18","3,18","1,19","2,19","3,19"],
    '_7':["1,1","2,1","3,1","4,1","5,1","6,1","7,1","8,1","9,1","10,1","1,2","2,2","3,2","4,2","5,2","6,2","7,2","8,2","9,2","10,2","0,3","9,3","10,3","9,4","8,5","9,5","8,6","8,7","7,8","8,8","7,9","7,10","6,11","7,11","6,12","6,13","5,14","6,14","5,15","4,16","5,16","4,17","5,17","4,18","13,18","3,19","4,19"],
    
    '8':["5,0","6,0","7,0","8,0","3,1","4,1","5,1","6,1","7,1","8,1","9,1","10,1","2,2","3,2","4,2","9,2","10,2","11,2","2,3","3,3","10,3","11,3","12,3","1,4","2,4","3,4","10,4","11,4","12,4","1,5","2,5","3,5","10,5","11,5","2,6","3,6","4,6","10,6","11,6","2,7","3,7","4,7","5,7","9,7","10,7","4,8","5,8","6,8","7,8","8,8","9,8","4,9","5,9","6,9","7,9","8,9","9,9","3,10","4,10","7,10","8,10","9,10","10,10","2,11","3,11","9,11","10,11","11,11","1,12","2,12","10,12","11,12","12,12","0,13","1,13","2,13","11,13","12,13","0,14","1,14","2,14","11,14","12,14","0,15","1,15","2,15","11,15","12,15","1,16","2,16","11,16","12,16","1,17","2,17","3,17","10,17","11,17","12,17","3,18","4,18","5,18","6,18","7,18","8,18","9,18","10,18","5,19","6,19","7,19","8,19"],
    '_8':["4,1","5,1","6,1","2,2","7,2","1,3","8,3","0,4","1,4","8,4","9,4","0,5","1,5","8,5","9,5","1,6","8,6","1,7","2,7","7,7","2,8","3,8","4,8","6,8","3,9","4,9","5,9","4,10","5,10","6,10","13,10","5,11","6,11","7,11","13,11","6,12","7,12","8,12","13,12","1,13","7,13","8,13","13,13","0,14","1,14","8,14","9,14","0,15","1,15","8,15","9,15","0,16","1,16","8,16","9,16","1,17","8,17","1,18","2,18","7,18","4,19","5,19"],
    
    '9':["5,0","6,0","7,0","3,1","4,1","5,1","6,1","7,1","8,1","9,1","2,2","3,2","9,2","10,2","11,2","1,3","2,3","10,3","11,3","1,4","2,4","11,4","12,4","0,5","1,5","2,5","11,5","12,5","0,6","1,6","2,6","11,6","12,6","0,7","1,7","2,7","11,7","12,7","0,8","1,8","2,8","11,8","12,8","1,9","2,9","3,9","11,9","12,9","2,10","3,10","10,10","11,10","12,10","3,11","4,11","5,11","6,11","7,11","8,11","9,11","11,11","12,11","5,12","6,12","7,12","11,12","12,12","11,13","12,13","11,14","12,14","10,15","11,15","10,16","11,16","1,17","8,17","9,17","10,17","1,18","2,18","3,18","4,18","5,18","6,18","7,18","8,18","9,18","3,19","4,19","5,19","6,19"],
    '_9':["4,1","5,1","7,2","8,3","0,4","9,4","0,5","9,5","0,6","9,6","10,6","0,7","9,7","10,7","0,8","9,8","10,8","0,9","9,9","10,9","0,10","1,10","9,10","10,10","1,11","2,11","8,11","9,11","3,12","4,12","5,12","9,12","8,13","8,14","7,15","6,16","5,17","3,18","4,18","0,19","1,19"]

}

var recognise = function(img){
    const canvas = document.createElement('canvas');
    const context = canvas.getContext('2d');
    const width = canvas.width = img.width = 60;
    const height = canvas.height = img.height = 30;
    context.drawImage(img, 0, 0);
    const imgData = context.getImageData(2, 4, width, height);
    for(let y = 0; y < imgData.height; y++){
        for(let x = 0; x < imgData.width; x++){
            const rIndex = (y * 4) * imgData.width + (x * 4);
            const gIndex = rIndex + 1;
            const bIndex = rIndex + 2;
            const r = imgData.data[rIndex]; 
            const g = imgData.data[gIndex]; 
            const b = imgData.data[bIndex];
            imgData.data[rIndex] = imgData.data[gIndex] = imgData.data[bIndex] = r+g+b > 360 ? 0 : 255; 
        }
    }

    context.putImageData(imgData, 0, 0);

    // document.body.appendChild(canvas)
    
    const code = []

    for(let i = 0; i < 4; i++){
        let _width = 14;
        let _x = i * _width;
        let _height = imgData.height;
        let xaxis = _width;
        let singleImgData = context.getImageData(_x, 0, _width, _height);
        let compare = true;
        for(let y = 0; y < singleImgData.height; y++){
            for(let x = 0; x < singleImgData.width; x++){
                const rIndex = (y * 4) * singleImgData.width + (x * 4);
                const gIndex = rIndex + 1;
                const bIndex = rIndex + 2;
                if(singleImgData.data[rIndex] === 255 && singleImgData.data[gIndex] === 255 && singleImgData.data[bIndex] === 255){
                    xaxis = Math.min(xaxis, x)
                    if(xaxis === 0){
                        compare = false
                        break;
                    }
                }
            }
            if(!compare){
                break;
            }
        }

        if(xaxis > 0){
            singleImgData = context.getImageData(_x + xaxis, 0, _width - xaxis, _height);
        }

        // const canvas2 = document.createElement('canvas');
        // const context2 = canvas2.getContext('2d');
        // context2.drawImage(canvas, 0, 0);
        // context2.putImageData(singleImgData, 0, 0);
        // document.body.appendChild(canvas2)

        const axis = [];
        for(let y = 0; y < singleImgData.height; y++){
            for(let x = 0; x < singleImgData.width; x++){
                const rIndex = (y * 4) * singleImgData.width + (x * 4);
                const gIndex = rIndex + 1;
                const bIndex = rIndex + 2;
                if(singleImgData.data[rIndex] === 255 && singleImgData.data[gIndex] === 255 && singleImgData.data[bIndex] === 255){
                    axis.push(`${x},${y}`)
                }
            }
        }

        const matchs = []

        const matchMap = {}

        for(let num in axisMap){
            const axisItem = axisMap[num];
            const length = axis.length;
            let _axis = [].concat(axis);
            let count = 0;
            axisItem.forEach(v => {
                const index = _axis.indexOf(v);
                if(index !== -1){
                    _axis = _axis.slice(0, index).concat(_axis.slice(index + 1))
                    count++
                }
            })
            const match = parseInt(count / length * 100);
            matchs.push(match)
            matchMap[num] = match
        }

        const max = Math.max.apply(Math, matchs);

        for(let m in matchMap){
            if(max === matchMap[m]){
                code.push(m.toString().replace(/^_/, ''))
                break;
            }
        }
    }

    return code.join('')
}